package cn.zj.logistics.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.zj.logistics.mapper.OrderDetailMapper;
import cn.zj.logistics.mapper.OrderMapper;
import cn.zj.logistics.pojo.Order;
import cn.zj.logistics.pojo.OrderDetail;
import cn.zj.logistics.pojo.OrderDetailExample;
import cn.zj.logistics.pojo.OrderDetailExample.Criteria;
import cn.zj.logistics.pojo.OrderExample;
import cn.zj.logistics.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService {
	
	@Autowired
	private OrderMapper orderMapper;
	
	
	@Autowired
	private OrderDetailMapper orderDetailMapper;
	
	@Override
	public int deleteByPrimaryKey(Long orderId) {
		return orderMapper.deleteByPrimaryKey(orderId);
	}
	
	
	/*
	 * 
	 * 新增订单{业务} 包含两个功能
	 * 
	 * 1，将订单的基本信息插入的 t_order表中
	 * 2，将订单的明细数据插入到t_order_detail
	 * 
	 */
	@Override
	public int insert(Order record) {
		
		// 1，将订单的基本信息插入的 t_order表中
		System.out.println("插入前："+record);
		int row = orderMapper.insert(record);
		System.out.println("插入后："+record);
		//2.订单明细依附订单表（插入订单同时先获取订单的id，设置给订单明细以后，再将订单明细插入数据表中）
		
		//获取订单明细
		List<OrderDetail> orderDetails = record.getOrderDetails();
		for (OrderDetail orderDetail : orderDetails) {
			//将新增订单生成的id设置给订单明细（订单明细属于当前订单）
			orderDetail.setOrderId(record.getOrderId());
			System.out.println(orderDetail);
			//新增订单明细
			orderDetailMapper.insert(orderDetail);
		}
		
		
		
		return row;
		
	}

	@Override
	public List<Order> selectByExample(OrderExample example) {
		return orderMapper.selectByExample(example);
	}

	@Override
	public Order selectByPrimaryKey(Long orderId) {
		return orderMapper.selectByPrimaryKey(orderId);
	}

	@Override
	public int updateByPrimaryKeySelective(Order record) {
		return orderMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public List<OrderDetail> selectOrderDetailByOrderId(Long orderId) {
		
		//根据订单id查询出对应的订单明细
		OrderDetailExample example = new OrderDetailExample();
		Criteria criteria = example.createCriteria();
		criteria.andOrderIdEqualTo(orderId);
		List<OrderDetail> details = orderDetailMapper.selectByExample(example );
		
		return details;
	}

}
